<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 */
class UsersController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

	function beforeFilter() {

		parent::beforeFilter();
		$this->Auth->allow('add', 'logout' ,'forgot');
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->Paginator->paginate());
	}

	public function login() {
	    if ($this->request->is('post')) {
	    	//debug($this->request->data);die();
	        if ($this->Auth->login()) {
	            return $this->redirect($this->Auth->redirectUrl());
	        } else {
	            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
	        }
	    }
	    $this->layout = 'login';
	}
    
    public function forgot() {
	    if ($this->request->is('post')) {
            $email = $this->request->data['User']['email'];
            //echo $email;exit;
            $user =  $this->User->find('first', array('conditions' => array('User.email' => $email)));
            if ($user != null && isset($user)){
               // var_dump( $user);
                //exit;
                $actikey = md5($user['User']['email']).rand(1,10);
                $this->User->read(null,$user['User']['id']);
                $this->User->set('actikey',$actikey);
                $this->User->save();
                //echo $user['User']['actikey'];exit;
                $message = '
                 <table cellpadding="0" cellspacing="0" border="0" width="100%">
                  <tbody>
                   <tr>
                    <td style="padding:10px;">
                     <p style="font-family:\'helvetica neue\', Arial, Helvetica, sans-serif; color: #666; font-weight:200; font-size: 18px; text-align:center;">
                      Hey there '.$user['User']['first_name'].'
                     </p>
                     <p style="font-family:\'helvetica neue\', Arial, Helvetica, sans-serif; color: #666; font-weight:200; font-size: 14px; text-align:center; padding:0px; margin:5px 0px 10px 0px;">We are so excited to have you as part of the YouLook community!</p>
                     <p style="font-family:\'helvetica neue\', Arial, Helvetica, sans-serif; color: #666; font-weight:200; font-size: 14px; text-align:center; padding:0px; margin:5px 0px 10px 0px;">Your account information is as follows:</p>
                    </td>
                   </tr>
                   <tr>
                    <!-- Class Data -->
                    <td style="padding:0px 100px;">
                     <table cellpadding="0" cellspacing="0" border="0" style="width:100%;" align="center">
                      <tbody>
                       <tr>
                        <!-- Day of the Week -->
                        <td style=" width:50%;padding:7px 10px 7px 5px; border-right: 1px solid #e6e6e6; border-bottom: 1px solid #e6e6e6; text-align:right;">
                         <span style="font-family:\'helvetica neue\', Arial, Helvetica, sans-serif; color: #ccc; font-weight:200; font-size: 12px; text-align:right; text-transform: uppercase;">First Name</span>
                        </td>
                        <td style=" width:50%;padding:7px 5px 7px 10px; border-bottom: 1px solid #e6e6e6;"><span style="color: rgb(153, 153, 153); font-family: \'helvetica neue\', Arial, Helvetica, sans-serif; font-size: 14px;">'.$user['User']['first_name'].'</span></td>
                       </tr>
                       <tr>
                        <!-- Time -->
                        <td style=" width:50%;padding:7px 10px 7px 5px; border-right: 1px solid #e6e6e6;  text-align:right;">
                         <span style="font-family:\'helvetica neue\', Arial, Helvetica, sans-serif; color: #ccc; font-weight:200; font-size: 12px; text-align:right; text-transform: uppercase;">Last Name</span>
                        </td>
                        <td style=" width:50%;padding:7px 5px 7px 10px; "><span style="color: rgb(153, 153, 153); font-family: \'helvetica neue\', Arial, Helvetica, sans-serif; font-size: 14px;">'.$user['User']['last_name'].'</span></td>
                       </tr>
                      </tbody>
                     </table>
                    </td>
                   </tr>
                   <!-- End Class Data -->
                   <tr>
                    <td style="padding: 30px 0px;">
                      <p style="text-align:center; padding:0px; margin:0px;">
                     <a href="http://'.$_SERVER["SERVER_NAME"].'/cakephp/forgots/active?active='.$user['User']['actikey'].'" style="margin-top: 10px;background-color: #79bd42;border: 1px solid #5d9f28;color: #fff;text-decoration: none;font-size: 22px;font-weight: bold;padding: 10px 40px;-moz-border-radius: 5px;-webkit-border-radius: 5px;border-radius: 5px;text-shadow: 2px 2px 2px #5e9533;">
                      Click here to change your password
                     </a>
                    </p>
                    </td>
                   </tr>
                  </tbody>
                 </table>
                 ';
                 //Debugger::dump($message); exit;
     $headers = "From: admin@gmail.com \r\n".
      "Reply-To: admin@gmail.com \r\n".
      'X-Mailer: PHP/' . phpversion();
     $headers  .= 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=utf8' . "\r\n";
     mail($email, 'Get your password', $message, $headers);
            }
            else{
                    echo 'dd';exit;
                $this->Session->setFlash(__('Can not find this Email in our system. Please try again.'));
            }
            
          //  echo $user['email'];exit;
	    }
	    $this->layout = 'login';
	}

    public function active() {
            $actikey=$this->params['url']['active'];
            //echo  $actikey;exit;
            $user = $this->User->find('first', array('conditions' => array('User.actikey' => $actikey)));
            
            //Debugger::dump($user);exit;
            //$this->render('active');
            
	}
	public function logout() {
		return $this->redirect($this->Auth->logout());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
		$this->set('user', $this->User->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			if (!isset($this->request->data['User']['role'])) $this->request->data['User']['role'] = 'user';
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved.'));
				return $this->redirect(array('action' => 'login'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
	}

/**
 * settings method
 *
 * @throws NotFoundException
 * @return void
 */
	public function settings() { 
		//echo 'ddd';exit;
		if ($this->request->is(array('post', 'put'))) {
			//debug($this->request->data);die();
			$this->User->id = $this->request->data['User']['id'];
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved.'));
				//return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id'))));
			unset($this->request->data['User']['password']);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->User->delete()) {
			$this->Session->setFlash(__('The user has been deleted.'));
		} else {
			$this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}}
